//
//  CustomDrawView.m
//  learnDrawYuanHu
//
//  Created by xiaomei su on 2018/3/14.
//  Copyright © 2018年 xuweixin. All rights reserved.
//

#import "CustomDrawView.h"

@interface CustomDrawView()
@property (nonatomic, assign) NSInteger number;
@property (nonatomic, strong) CAShapeLayer *layer2;
@property (nonatomic, assign) CGPoint centerPoint;
@end

@implementation CustomDrawView

-(id)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        self.number = -1;
        self.layer2 = [CAShapeLayer new];
        self.centerPoint = self.center;
        
        CAShapeLayer *layer1 = [CAShapeLayer new];
        layer1.lineWidth = 8;
        //圆环的颜色
        layer1.strokeColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.9].CGColor;
        //背景填充色
        layer1.fillColor = [UIColor clearColor].CGColor;
        //设置半径为10
        CGFloat radius = self.frame.size.height/2;
        //按照顺时针方向
        BOOL clockWise = true;
        //初始化一个路径
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:self.center radius:radius startAngle:(0.75*M_PI) endAngle:0.25*M_PI clockwise:clockWise];
        layer1.path = [path CGPath];
        [self.layer addSublayer:layer1];
//        [self testWithNumber:1];
//        [self.layer addSublayer:layer1];
    }
    return self;
}
//-(void)setLayer:(CAShapeLayer *)layer{

    
//}


-(void)testWithNumber:(NSInteger)number{
//    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
//    gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor, (__bridge id)[UIColor yellowColor].CGColor, (__bridge id)[UIColor blueColor].CGColor];
//    gradientLayer.locations = @[@0.3, @0.5, @1.0];
//    gradientLayer.startPoint = CGPointMake(0, 0);
//    gradientLayer.endPoint = CGPointMake(1.0, 0);
//    gradientLayer.frame = CGRectMake(0, 100, 300, 100);
//    [self.view.layer addSublayer:gradientLayer];
    BOOL result = NO;
    float start;
    float end;
    if (number >0) {
        number = number -1;
        result = YES;
    }
    if (0.1*number + 0.75 >= 1.95) {
//        if (0.1*number + 0.75 == 1.95){
//            start = (0.1 *number + 0.75 - 1.95)*M_PI;
//            end = start + 0.1*M_PI;
//        }else{
            start = (0.1 *number + 0.75 - 2.0)*M_PI;
            end = start + 0.1*M_PI;
//        }
    } else{
        start = (0.1 *number + 0.75)*M_PI;
        end = start + 0.1*M_PI;
        
    }
    NSLog(@"%f",0.1 *number + 0.75);
    self.layer2.lineWidth = 10;
    //圆环的颜色
    if (result) {
        self.layer2.strokeColor = [UIColor greenColor].CGColor;
    }else{
        self.layer2.strokeColor = [UIColor clearColor].CGColor;

    }
    //背景填充色
    self.layer2.fillColor = [UIColor clearColor].CGColor;
    //设置半径为10
    CGFloat radius = self.frame.size.height/2;
    //按照顺时针方向
    BOOL clockWise = true;
    //初始化一个路径
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:self.centerPoint radius:radius startAngle:start endAngle:end clockwise:clockWise];
    self.layer2.path = [path CGPath];
    [self.layer addSublayer:self.layer2];
}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    NSLog(@"x:%f,y:%f",point.x,point.y);
//    CGFloat height = 0.35 *[UIScreen mainScreen].bounds.size.height;
//    CGFloat biaoZhunHeight = 0.38 *[UIScreen mainScreen].bounds.size.height;
//    if (height < point.y  ) {
//        if (point.x < 0.5*biaoZhunHeight) {
//            point = CGPointMake(0, height);
//        }else{
//            point = CGPointMake(biaoZhunHeight, height);
//        }
//    }
//    if (point.y < height) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(CustomDrawViewMoveLocation:)]) {
            [self.delegate CustomDrawViewMoveLocation:point];
        }
//    }
    
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
//    CGFloat height = 0.35 *[UIScreen mainScreen].bounds.size.height;
//    CGFloat biaoZhunHeight = 0.38 *[UIScreen mainScreen].bounds.size.height;
//    if (height < point.y  ) {
//        if (point.x < 0.5*biaoZhunHeight) {
//            point = CGPointMake(0, height);
//        }else{
//            point = CGPointMake(biaoZhunHeight, height);
//        }
//    }
    NSLog(@"x:%f,y:%f",point.x,point.y);
//    CGFloat height = self.frame.size.height*0.9;
//    if (point.y < height) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(CustomDrawViewMoveEndLocation:)]) {
            [self.delegate CustomDrawViewMoveEndLocation:point];
        }

//    }
    
}

@end
